
package net.biaji.android.alephclient;

import net.biaji.alephclient.AgentFactory;
import net.biaji.alephclient.LibAgent;
import android.os.Handler;
import android.util.Log;

public class AgentSingleton {

    private static LibAgent agent;

    private AgentSingleton() {

    }

    public static LibAgent getInstance() {

        if (agent == null) {
            agent = AgentFactory.getAgent(AgentFactory.AGENT_TYPE_WEB);
        }
        synchronized (agent) {
            return agent;
        }

    }

    public static LibAgent getInstance(final String name, final String password,
            final Handler handler) {

        if (agent == null) {
            agent = AgentFactory.getAgent(AgentFactory.AGENT_TYPE_WEB);
            new Thread() {
                public void run() {
                    Log.d("LOGIN", "begin Login");
                    boolean loginResult = agent.login(name, password);
                    Log.d("LOGIN", "result:" + loginResult);
                    handler.sendEmptyMessage(0);
                }
            }.start();

        }
        return agent;
    }

    public static LibAgent getInstance(final String name, final String password) {

        if (agent == null) {
            agent = AgentFactory.getAgent(AgentFactory.AGENT_TYPE_WEB);

            Log.d("LOGIN", "begin Login");
            boolean loginResult = agent.login(name, password);
            Log.d("LOGIN", "result:" + loginResult);

        }
        return agent;
    }
}
